package gui;

import houseHold.Room;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import ObjectInfo.ObjectTableModel;

public class ObjectInfoDialog extends JDialog {

	public static boolean canMove = false;
	public static JTable ObjectList = null;
	private static final long serialVersionUID = 1L;
	private static JDialog WarningDialog = null; // @jve:decl-index=0:visual-constraint="871,148"

	public static void ShowWarning() {
		ObjectList.setEnabled(false);
		WarningDialog.pack();
		Point loc = ObjectList.getLocation();
		loc.translate(50, 40);
		WarningDialog.setLocation(loc);
		WarningDialog.setSize(new Dimension(320, 160));
		WarningDialog.setVisible(true);
	}

	private Room callingRoom; // @jve:decl-index=0:
	private JButton Cancel = null;
	private JPanel jContentPane = null;
	private JPanel OIPanel = null;
	private JButton Ok = null;
	private Frame owner;
	private JScrollPane TableScrollPanel = null;
	private JPanel Warning = null;
	private JLabel WarningLabel = null;

	private JLabel WL = null;

	// @jve:decl-index=0:visual-constraint="493,135"
	/**
	 * @param owner
	 */
	public ObjectInfoDialog(Frame owner) {
		super(owner);
		this.owner = owner;
		WarningDialog = getWarningDialog();

	}

	/**
	 * This method initializes Cancel
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancel() {
		if (Cancel == null) {
			Cancel = new JButton();
			Cancel.setBounds(new Rectangle(157, 80, 100, 25));
			Cancel.setText("Cancel");
			Cancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					canMove = false;
					WarningDialog.setVisible(false);
					ObjectList.setEnabled(true);
				}
			});
		}
		return Cancel;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(Color.white);
			jContentPane.add(getOIPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes ObjectInfo
	 * 
	 * @return javax.swing.JTable
	 */
	/*
	 * callingObject=new ElectricObject(); callingRoom=new Room();
	 * callingRoom.addSensor(new Sensor());
	 * callingObject.setLocation(callingRoom);
	 * House.getInstance().addRoom(callingRoom); callingObject.setSensor(new
	 * Sensor()); ObjectData.initAttribs(); ObjectData od=new ObjectData();
	 * od.getData(callingObject); ObjectTableModel otm=new ObjectTableModel();
	 * otm.init(); otm.isEditable=true; otm.setData(od.init()); ObjectInfo = new
	 * JTable(otm); ObjectInfo.setPreferredScrollableViewportSize(new
	 * Dimension(500, 40)); ObjectInfo.setFillsViewportHeight(true);
	 * od.setUpConsumptionColumn(ObjectInfo,
	 * ObjectInfo.getColumnModel().getColumn(5),callingRoom);
	 * od.setUpObjectTypeColumn(ObjectInfo,
	 * ObjectInfo.getColumnModel().getColumn(1), od.cType);
	 * od.setUpLocationColumn(ObjectInfo,
	 * ObjectInfo.getColumnModel().getColumn(2));
	 * od.setUpSensorColumn(ObjectInfo,
	 * ObjectInfo.getColumnModel().getColumn(3),callingRoom);
	 * ObjectInfo.setRowHeight(20); ObjectInfo.setFont(new Font("SansSerif",
	 * Font.BOLD, 14));
	 */

	private JTable getObjectList() {
		if (ObjectList == null) {
			ObjectTableModel otModel = new ObjectTableModel(callingRoom);
			ObjectList = new JTable(otModel);
			otModel.updateTable(ObjectList);
			otModel.setTable(ObjectList);
			ObjectList.setRowHeight(20);
			ObjectList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			ObjectList.setCellSelectionEnabled(true);
			ObjectList.setDragEnabled(false);
		}
		return ObjectList;
	}

	/**
	 * This method initializes OIPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOIPanel() {
		if (OIPanel == null) {
			OIPanel = new JPanel();
			OIPanel.setLayout(new BorderLayout());
			OIPanel.setBounds(new Rectangle(10, 10, 760, 240));
			OIPanel.add(getTableScrollPanel(), BorderLayout.CENTER);
			OIPanel.setBackground(Color.white);
			OIPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
					.createBevelBorder(BevelBorder.RAISED), BorderFactory
					.createTitledBorder(BorderFactory.createLineBorder(
							Color.gray, 5), "Object List",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));
		}
		return OIPanel;
	}

	/**
	 * This method initializes Ok
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOk() {
		if (Ok == null) {
			Ok = new JButton();
			Ok.setBounds(new Rectangle(30, 80, 100, 25));
			Ok.setText("Continue");
			Ok.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					canMove = true;
					WarningDialog.setVisible(false);
					ObjectList.setEnabled(true);
				}
			});
		}
		return Ok;
	}

	private JScrollPane getTableScrollPanel() {
		if (TableScrollPanel == null) {
			TableScrollPanel = new JScrollPane();
			TableScrollPanel.setViewportView(getObjectList());
		}
		return TableScrollPanel;
	}

	/**
	 * This method initializes Warning
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getWarning() {
		if (Warning == null) {
			WarningLabel = new JLabel();
			WarningLabel.setBounds(new Rectangle(30, 15, 245, 50));
			WarningLabel.setHorizontalAlignment(SwingConstants.CENTER);
			WarningLabel.setHorizontalTextPosition(SwingConstants.CENTER);
			WarningLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
			WarningLabel
					.setText("<html>Warning:<br>Moving object to another room <br>will cause it to be removed from the list</html>");
			Warning = new JPanel();
			Warning.setLayout(null);
			Warning.add(getOk(), null);
			Warning.add(getCancel(), null);
			Warning.add(WarningLabel, null);
		}
		return Warning;
	}

	/**
	 * This method initializes WarningDialog
	 * 
	 * @return javax.swing.JDialog
	 */
	private JDialog getWarningDialog() {
		if (WarningDialog == null) {
			WarningDialog = new JDialog(this);
			WarningDialog.setBounds(new Rectangle(0, 0, 320, 160));
			WarningDialog.setTitle("Warning!");
			WarningDialog.setContentPane(getWarning());
		}
		return WarningDialog;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void initialize(Room r) {
		ObjectList = null;
		callingRoom = r;
		this.setSize(800, 300);
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent e) {
				owner.setEnabled(true);
			}
		});
	}

}
